<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <title>用户登录</title>
    <link href="{{ see_config('favicon') }}" rel="Shortcut Icon">
    <link rel="stylesheet" type="text/css" href="{{ lib('layui/css/layui.css') }}">
    <link rel="stylesheet" type="text/css" href="{{ static('css/style.css') }}">
    <link rel="stylesheet" type="text/css" href="{{ static('css/mb.css') }}"/>
    <style type="text/css">
        body {
            background-repeat: no-repeat;
            background-color: #f1f4fd;
            background-size: cover;
        }

        #login-scan img {
            display: block;
            margin-left: auto;
            margin-right: auto;
            max-width: 260px;
            max-height: 260px;
        }
    </style>
</head>

<body>
<div>
    <div class="login-main">

        <div class="layui-row" style="display: flex">
            <div class="layui-col-md6">
                <div style="background: url({{ static('img/login-banner.jpg') }}) no-repeat center center;height: 100%;background-size:cover;">
                    <p></p>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="mtb10 p20 layui-font-28 login-title" style="color: #454d56;">
                    {{ __('login.welcome') }}
                </div>
                <div class="login-container">
                    <form class="layui-form" method="post" action="">

                        <input type="hidden" name="type" value="{{ type }}">

                        <div id="login-input">
                            {% include type_file %}
                        </div>
                        <div id="login-scan" class="layui-hide">
                            <img style="display: block;margin-left: auto;margin-right: auto;"
                                 src="">
                            <div class="layui-form-item">
                                <div class="layui-input-wrap">
                                    <input type="text"
                                           name="token"
                                           autocomplete="off"
                                           value=""
                                           class="layui-input"
                                           lay-affix="clear">
                                </div>
                            </div>
                            <div class="layui-form-item login-btn">
                                <button type="button" class="layui-btn layui-btn-normal layui-btn-fluid" lay-submit
                                        lay-filter="submit">
                                    {{ __('login.btn') }}
                                </button>
                            </div>
                        </div>
                    </form>
                </div>

                <div class="m30">
                    {% if support|length > 2 %}

                        <div>
                            <fieldset class="layui-elem-field layui-field-title layui-word-aux">
                                <legend class="layui-font-12"
                                        style="margin: 0 auto;"> {{ __('login.other_login') }}</legend>
                            </fieldset>
                        </div>

                        <div class="layui-breadcrumb text-center p10" lay-separator="|" id="login-support">

                            <a href="javascript:;" data-type="1">
                                <span class="{% if not type or type == 1 %}layui-font-blue{% endif %}"> {{ __('login.method_username_passwd') }}</span>
                            </a>

                            {% for key,item in support_login_method %}
                                <a href="javascript:;" data-type="{{ key }}">
                                    <span class="{% if type == key %}layui-font-blue{% endif %}">{{ item }}</span>
                                </a>
                            {% endfor %}

                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
<script src="{{ see_config('lang.static.load') }}/{{ seecms_lang }}.js"></script>
<script src="{{ lib('jquery/jquery-3.1.0.min.js') }}?v=v1.12.4"></script>
<script src="{{ lib('jquery/jquery.cookie.js') }}?v=v3.4.4."></script>
<script type="text/javascript" src="{{ lib('layui/layui.js') }}?v=v2.9.4 "></script>

{% include 'layout/layui.twig' %}
<script type="text/javascript">
    layui.use(['form', 'http', 'toast'], function () {
        let form = layui.form;
        let toast = layui.toast;
        let http = layui.http;

        let login_type = '{{ type }}';
        let flag = '{{ wx_flag }}';

        let sms_code_len = parseInt('{{ sms_code_len|default(4) }}');
        if (isNaN(sms_code_len) || sms_code_len < 4 || sms_code_len > 6) {
            sms_code_len = 4;
        }

        $('#login-support a').on('click', function () {
            let type = $(this).data('type');
            window.location.href = '{{ auth_route('user/login') }}?type=' + type;
            return false;
        });

        form.on('submit(submit)', function (data) {

            let type = data.field.type || '1';

            if (type === '1') {
                let username = data.field.username;
                if (!username) {
                    toast.top(lang.login.username_password_null || '请输入登录账号或密码');
                    return false;
                }

                if (username.length < 4 || username.length > 32) {
                    toast.top(lang.login.username_length_error || '登录账号长度错误');
                    return false;
                }
                let password = data.field.password;
                if (!/^\S{6,18}$/.test(password)) {
                    toast.top(lang.login.password_length_error || '密码是6~18位，且不能出现空格');
                    return false;
                }

                let verify = data.field.verify;
                if (!/^\S{4,6}$/.test(verify)) {
                    toast.top(lang.login.verify_code_error || '请输入4~6位图形验证码');
                    return false;
                }

            } else if (type === '2') {
                let phone = data.field.phone;
                if (!/^1\d{10}$/.test(phone)) {
                    toast.top(lang.login.phone_format_error || '手机号码格式不正确');
                    return false;
                }
                let code = data.field.code;
                if (code.length < sms_code_len || code.length > sms_code_len) {
                    toast.top(lang.login.enter_code_error || '动态验证码错误');
                    return false;
                }
            } else if (type === '2') {
                data.field.token = $('[name=token]').val();
                data.field.user_id = 2;
            }

            http.post('{{ auth_route('user/doLogin') }}', data.field).then(res => {
                if (typeof res === 'object') {
                    if (res.code) {
                        toast.top(res.msg ? res.msg : (lang.login.error || '登录出现错误'));
                    } else {
                        toast.top(lang.login.success || '登录成功', true);
                        setTimeout(function () {
                            window.location = '{{ auth_route('home/index') }}';
                        }, 2000);
                    }
                } else {
                    console.log(res);
                }
            });
            return false;
        });

        // 微信扫码登录
        if (login_type === '4') {

            setInterval(function () {

                http.post('{{ auth_route('user/wxScan') }}', {
                    flag: flag
                }).then(res => {

                    if (typeof res === 'object') {

                        if (res.data === 2) {
                            return false;
                        }

                        if (res.code) {
                            toast.top(res.msg ? res.msg : (lang.login.error || '登录出现错误'));
                        } else {
                            toast.top(lang.login.success || '登录成功', true);
                            setTimeout(function () {
                                window.location = '{{ auth_route('home/index') }}';
                            }, 2000);
                        }
                    } else {

                    }
                });
            }, 4000);
        }
    });
</script>
</body>

</html>